package com.sec.android.app.myfiles.feature;

import android.app.Activity;
import android.content.Context;

import com.sec.android.app.myfiles.MyFilesTestRunner;
import com.sec.android.app.myfiles.TestInput;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.shadow.android.ShadowStorageManager;
import com.sec.android.app.myfiles.util.StorageMonitor;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.reflect.internal.WhiteboxImpl;
import org.robolectric.Robolectric;

/**
 * Created by jaebae on 2016-11-14.
 */
@RunWith(MyFilesTestRunner.class)
public class testDragAndDropMgr {
    private Context mContext;

    @Before
    public void setUp() throws Exception {
        mContext = Robolectric.buildActivity(Activity.class).create().get();
    }

    @After
    public void tearDown() throws Exception {

    }

    @Test
    public void testIsDragAndDropAvailable() throws Exception {
        TestInput[] testList = TestInput.createTestData(
                new TestInput<>(false, null, null),
                new TestInput<>(true, AppConstants.StoragePath.PRIVATE_ROOT, AppConstants.StoragePath.PRIVATE_ROOT),
                new TestInput<>(false, AppConstants.StoragePath.PRIVATE_ROOT, AppConstants.StoragePath.INTERNAL_ROOT),
                new TestInput<>(true, AppConstants.StoragePath.INTERNAL_ROOT, AppConstants.StoragePath.PRIVATE_ROOT),
                new TestInput<>(true, AppConstants.StoragePath.PRIVATE_ROOT + "/test", AppConstants.StoragePath.PRIVATE_ROOT + "/test"),
                new TestInput<>(false, AppConstants.StoragePath.PRIVATE_ROOT + "/test", AppConstants.StoragePath.INTERNAL_ROOT + "/test"),
                new TestInput<>(true, AppConstants.StoragePath.INTERNAL_ROOT + "/test", AppConstants.StoragePath.PRIVATE_ROOT + "/test"),


                new TestInput<>(true, AppConstants.StoragePath.INTERNAL_ROOT, AppConstants.StoragePath.INTERNAL_ROOT),
                new TestInput<>(true, AppConstants.StoragePath.INTERNAL_ROOT, StorageMonitor.getExtSDCardPath()),
                new TestInput<>(true, AppConstants.StoragePath.INTERNAL_ROOT, "/mnt/usbA"),
                new TestInput<>(true, AppConstants.StoragePath.INTERNAL_ROOT + "/test", AppConstants.StoragePath.INTERNAL_ROOT + "/test"),
                new TestInput<>(true, AppConstants.StoragePath.INTERNAL_ROOT + "/test", StorageMonitor.getExtSDCardPath() + "/test"),
                new TestInput<>(true, AppConstants.StoragePath.INTERNAL_ROOT + "/test", "/mnt/usbA/test"),

                new TestInput<>(false, AppConstants.StoragePath.INTERNAL_ROOT + "A", AppConstants.StoragePath.INTERNAL_ROOT),
                new TestInput<>(false, AppConstants.StoragePath.INTERNAL_ROOT, AppConstants.StoragePath.INTERNAL_ROOT + "A"),
                new TestInput<>(false, AppConstants.StoragePath.INTERNAL_ROOT, StorageMonitor.getExtSDCardPath() + "A"),
                new TestInput<>(false, AppConstants.StoragePath.INTERNAL_ROOT, "/mnt/usbB"),

                new TestInput<>(true, StorageMonitor.getExtSDCardPath(), AppConstants.StoragePath.INTERNAL_ROOT),
                new TestInput<>(true, "/mnt/usbA", AppConstants.StoragePath.INTERNAL_ROOT),
                new TestInput<>(true, StorageMonitor.getExtSDCardPath(), AppConstants.StoragePath.PRIVATE_ROOT),
                new TestInput<>(true, "/mnt/usbA", AppConstants.StoragePath.PRIVATE_ROOT)
        );

        ShadowStorageManager.addStorageVolumes(AppConstants.EXT_SD_CARD_ROOT);
        ShadowStorageManager.addStorageVolumes("/mnt/usbA");
        StorageMonitor.updateSubSystemStorageState(mContext);

        for (TestInput<Boolean> testInput : testList) {
            boolean actual = WhiteboxImpl.invokeMethod(DragAndDropMgr.class, "isDragAndDropAvailable", testInput.get(0), testInput.get(1));
            boolean excepted = testInput.expected();
            String msg = testInput.get(0) + " / " + testInput.get(1);
            Assert.assertEquals(msg, excepted, actual);
        }

        /*unmounted sd card test*/
        ShadowStorageManager.clearStorageVolumes();
        StorageMonitor.updateSubSystemStorageState(mContext);
        boolean actual = WhiteboxImpl.invokeMethod(DragAndDropMgr.class, "isDragAndDropAvailable", AppConstants.StoragePath.INTERNAL_ROOT, StorageMonitor.getExtSDCardPath());
        Assert.assertFalse(actual);
    }
}
